/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

namespace OpenServers.Net.Common.FileSystem {
    /// <summary>
    /// An abstract file system.
    /// </summary>
    public abstract class AbstractFileSystem : IFileSystem {
        private readonly IDirectory _rootDir;

        /// <summary>
        /// constructor.
        /// </summary>
        public AbstractFileSystem(IDirectory rootDir) {
            _rootDir = rootDir;
        }

        /// <summary>
        /// Gets a file from the file system.
        /// </summary>
        /// <param name="fileName">full path of the file seperated by '/'</param>
        public IFile GetFile(string fileName) {
            IFileSystemObject result = RootDirectory.GetObject(fileName);
            if (result is IFile) {
                return (IFile)result;
            }
            return null;
        }

        /// <summary>
        /// Gets a directory from the file system.
        /// </summary>
        /// <param name="path">full path seperated by '/'</param>
        public IDirectory GetDirectory(string path) {
            IFileSystemObject result = RootDirectory.GetObject(path);
            if (result is IDirectory) {
                return (IDirectory)result;
            }
            return null;
        }

        /// <summary>
        /// Gets an object from the file system.
        /// </summary>
        /// <param name="path">full path seperated by '/'</param>
        public IFileSystemObject GetObject(string path) {
            return RootDirectory.GetObject(path);
        }

        /// <summary>
        /// The root directory of the file system.
        /// </summary>
        public IDirectory RootDirectory {
            get { return _rootDir; }
        }
    }
}
